package fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.boltshow.BoltDetailsActivity;
import com.example.administrator.boltshow.CatIDActivity;
import com.example.administrator.boltshow.LoginActivity;
import com.example.administrator.boltshow.R;
import com.example.administrator.boltshow.viewone.MyDialog;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import adapter.BoltBaseAdapter;
import application.MyApplication;
import config.NetConfig;
import model.BoltHomeModel;

/**
 * Created by Administrator on 2016/8/29.
 */
public class BoltFragment extends Fragment implements View.OnClickListener {
    private ListView mListView;
    private TextView mTextViewHome,mTextViewSport,mTextViewColther,mTextViewShoes,mTextViewBeau,mTextViewMore;
    private ImageView mImageViewTitle, mImageViewOne, mImageViewTwo, mImageViewThree, mImageViewFour, mImageViewFive;
    private Toolbar mToolbar;
    private LinearLayout mLinearLayout, mLinearLayoutTitle;
    private ImageView mImageViewRight, mImageViewLeft;
    private List<BoltHomeModel.DataBean> mData;
    private BoltBaseAdapter mAdapter;
    private ImageOptions mImageOptions;
    private List<BoltHomeModel.TemplatesBean> mTitles;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bolt, null);
        initView(view);
        initData();
        setData();
        setLintener();
        return view;
    }

    private void initView(View view) {
        mTextViewHome = (TextView) view.findViewById(R.id.tv_fragment_bolt_home);
        mTextViewSport= (TextView) view.findViewById(R.id.tv_fragment_bolt_sport);
        mTextViewColther= (TextView) view.findViewById(R.id.tv_fragment_bolt_clother);
        mTextViewShoes= (TextView) view.findViewById(R.id.tv_fragment_bolt_shoes);
        mTextViewBeau= (TextView) view.findViewById(R.id.tv_fragment_bolt_beautiful);
        mTextViewMore= (TextView) view.findViewById(R.id.tv_fragment_bolt_more);
        mListView = (ListView) view.findViewById(R.id.rv_fragment_bolt_show);
        mToolbar = (Toolbar) view.findViewById(R.id.tl_fragment_bolt_toolbar);
        mImageViewLeft = (ImageView) view.findViewById(R.id.iv_fragment_bolt_left);
        mImageViewRight = (ImageView) view.findViewById(R.id.iv_fragment_bolt_right);
        mLinearLayoutTitle = (LinearLayout) view.findViewById(R.id.ll_fragment_bolt_containertitle);
        //设置头布局
        View viewhead = LayoutInflater.from(getActivity()).inflate(R.layout.view_head_bolt, null);
        //找到头布局里面的控件
        mImageViewTitle = (ImageView) viewhead.findViewById(R.id.iv_fragment_title);
        mImageViewOne = (ImageView) viewhead.findViewById(R.id.iv_fragment_bolt_one);
        mImageViewTwo = (ImageView) viewhead.findViewById(R.id.iv_fragment_bolt_two);
        mImageViewThree = (ImageView) viewhead.findViewById(R.id.iv_fragment_bolt_three);
        mImageViewFour = (ImageView) viewhead.findViewById(R.id.iv_fragment_bolt_four);
        mImageViewFive = (ImageView) viewhead.findViewById(R.id.iv_fragment_bolt_five);
        mLinearLayout = (LinearLayout) viewhead.findViewById(R.id.ll_fragment_bolt_container);
        mListView.addHeaderView(viewhead);
    }

    private void initData() {
        mTitles = new ArrayList<>();
        mData = new ArrayList<>();
        mAdapter = new BoltBaseAdapter(mData, getContext());
        mImageOptions = new ImageOptions.Builder().setUseMemCache(true).build();
        x.http().get(new RequestParams(NetConfig.BOLT_PATH), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result != null) {
                    Gson gson = new Gson();
                    BoltHomeModel boltHomeModel = gson.fromJson(result, BoltHomeModel.class);
                    //初始化HorizontalScrollView里面的数据
                    List<BoltHomeModel.TemplatesBean> templates = boltHomeModel.getTemplates();
                    mTitles.addAll(templates);
                    List<BoltHomeModel.TemplatesBean.ItemBean> item = templates.get(0).getItem();
                    for (int i = 0; i < item.size(); i++) {
                        final String catId = item.get(i).getLink();
                        final String catName = item.get(i).getName();
                        ImageView imageView = new ImageView(getActivity());
                       LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,1f);
                        imageView.setLayoutParams(params);
                         imageView.setTop(5);
                        imageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //点击图片跳转到其他页面,需要把catId catName 传过去
                                Bundle bundle = new Bundle();
                                bundle.putString("catId", catId);
                                bundle.putString("catName", catName);
                                Intent intent = new Intent(getActivity(), CatIDActivity.class);
                                intent.putExtra("bundle", bundle);
                                getActivity().startActivity(intent);
                            }
                        });
                        x.image().bind(imageView, item.get(i).getImgUrl(), mImageOptions);
                        mLinearLayout.addView(imageView);
                    }
                    //初始化adapter
                    mData.addAll(boltHomeModel.getData());
                    mAdapter.notifyDataSetChanged();
                    //初始❀五张图片
                    List<BoltHomeModel.TemplatesBean.ItemBean> item1 = templates.get(1).getItem();
                    //x.image().bind(mImageViewOne, item1.get(0).getImgUrl(), mImageOptions);
                    //x.image().bind(mImageViewTwo, item1.get(1).getImgUrl(), mImageOptions);
                   // x.image().bind(mImageViewThree, item1.get(2).getImgUrl(), mImageOptions);
                   // x.image().bind(mImageViewFour, item1.get(3).getImgUrl(), mImageOptions);
                    List<BoltHomeModel.TemplatesBean.ItemBean> item2 = templates.get(2).getItem();
                    x.image().bind(mImageViewFive, item2.get(0).getImgUrl(), mImageOptions);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "出错了", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void setData() {
        mListView.setAdapter(mAdapter);
    }

    private void setLintener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {

                } else {
                    //传linkid 过去
                    Bundle bundle = new Bundle();
                    bundle.putString("linkid", mData.get(position).getLinkId());
                    Intent intent = new Intent(getActivity(), BoltDetailsActivity.class);
                    intent.putExtra("bundle", bundle);
                    getActivity().startActivity(intent);
                }
            }
        });


        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem > 0) {
                    mLinearLayoutTitle.setVisibility(View.VISIBLE);
                } else {
                    mLinearLayoutTitle.setVisibility(View.GONE);
                }
            }
        });
        mImageViewRight.setOnClickListener(this);
        mImageViewLeft.setOnClickListener(this);
        mTextViewMore.setOnClickListener(this);
        mTextViewBeau.setOnClickListener(this);
        mTextViewShoes.setOnClickListener(this);
        mTextViewColther.setOnClickListener(this);
        mTextViewHome.setOnClickListener(this);
        mTextViewSport.setOnClickListener(this);
    }

    //创建一个dialog
    public void getDialog() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.pop_window, null);
        Button button = (Button) view.findViewById(R.id.btn_commit);

        final MyDialog dialog = new MyDialog(getActivity(), view, false);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v != null) {
                    dialog.dismiss();
                }
            }
        });
        dialog.show();
    }

    @Override
    public void onClick(View v) {

        if (v != null) {
            switch (v.getId()) {
                case R.id.iv_fragment_bolt_left:
                    getDialog();
                    break;
                case R.id.iv_fragment_bolt_right:
                    MyApplication application = (MyApplication) getActivity().getApplication();
                    if (application.getIsLogin()) {

                    } else {
                        Toast.makeText(getActivity(), "您还没有登录,请前去登录", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                    }
                    break;
                case R.id.tv_fragment_bolt_home:
                    break;
                case R.id.tv_fragment_bolt_sport:
                    jump(0);
                    break;
                case R.id.tv_fragment_bolt_clother:
                    jump(1);

                    break;
                case R.id.tv_fragment_bolt_shoes:
                    jump(2);

                    break;
                case R.id.tv_fragment_bolt_beautiful:
                    jump(3);

                    break;
                case R.id.tv_fragment_bolt_more:
                    jump(4);

                    break;
                default:
                    break;
            }
        }
    }

    public void jump(int i) {
        BoltHomeModel.TemplatesBean templatesBean = mTitles.get(0);
        List<BoltHomeModel.TemplatesBean.ItemBean> item = templatesBean.getItem();
        BoltHomeModel.TemplatesBean.ItemBean itemBean = item.get(i);
        String link = itemBean.getLink();
        String name = itemBean.getName();
        Bundle bundle = new Bundle();
        bundle.putString("catId", link);
        bundle.putString("catName", name);
        Intent intent = new Intent(getActivity(), CatIDActivity.class);
        intent.putExtra("bundle", bundle);
        getActivity().startActivity(intent);
    }

}
